// pages/menu/data-magic/order-statistics/search/search.js
var verify = require('../../../../../utils/verify-token.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 10,
    loading: true,
    filterValue: "",
    isFresh: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var filterType = options.filterType
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      filterType: filterType
    })
    if (filterType == "deviceFilter") {
      wx.setNavigationBarTitle({
        title: '选择设备',
      })
    }
    if (filterType == "locationFilter") {
      wx.setNavigationBarTitle({
        title: '选择点位',
      })
    }
    if (filterType == "companyFilter") {
      wx.setNavigationBarTitle({
        title: '选择公司',
      })
    }
  },

  // 获取设备列表
  getDataList(){
    // console.log("deviceList")
    var baseUrl = app.globalData.baseUrl
    var filterType = this.data.filterType
    
    var size = this.data.size
    var token = app.globalData.token
    var filterValue = this.data.filterValue
    var that = this
    var url1
    if (filterType == "deviceFilter") {
      url1 = baseUrl + 'devices/pagedSearch?productId=ALL&extendProperty=ALL&networkType=ALL&specs=ALL&netStatus=ALL&deviceStatus=ALL&filter=identifier&page=0&size=' + size + '&filterValue=' + filterValue
    }
    if (filterType == "locationFilter") {
      url1 = baseUrl + 'locations2?page=0&size=' + size +
        '&filter=locationName' +
        '&filterValue=' + filterValue
    }
    if (filterType == "companyFilter") {
      url1 = baseUrl + 'companies2?page=0&size=' + size +
        '&filter=companyName' +
        '&filterValue=' + filterValue
    }

    wx.request({
      url: url1,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var dataList = res.data.data.content

          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > size) {
            loading = true
          }
          console.log("dataList = ", dataList)
          that.setData({
            dataList: dataList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },
  inputTyping: function (e) {
    console.log(e.detail.value)
    this.setData({
      filterValue: e.detail.value
    });
    this.search();
  },

  search: function () {
    var filterValue = this.data.filterValue
    var size = 10
    this.setData({
      filterValue: filterValue,
      size: size
    })
    this.getDataList()
  },

  // 选择项目
  choose(res){
    var index = res.currentTarget.dataset.index;
    var dataList = this.data.dataList
    var filterType = this.data.filterType
    var values = ""
    if (filterType == "deviceFilter") {
      var filterValue = dataList[index].identifier;
      values = dataList[index].deviceId;
    }
    if (filterType == "locationFilter") {
      var filterValue = dataList[index].locationName 
      values = dataList[index].identifier 
    }
    if (filterType == "companyFilter") {
      var filterValue = dataList[index].companyName
      values = dataList[index].companyId
    }
    console.log(filterValue)
    wx.redirectTo({
      url: '../order-statistics?filterValue=' + filterValue + '&filterType=' + filterType + '&values=' + values,
    })
  },

  // 选择全部
  selectAll(){
    var filterType = this.data.filterType
    wx.redirectTo({
      url: '../order-statistics?filterValue=ALL&filterType=' + filterType,
    })
  },

  onShow: function () {
    this.getDataList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size * 1 + 10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size - 10 > totalElements) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        size: size
      })
      this.getDataList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})